home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / cmplib_s.lha / cmplib_src / $flatten1.P < prev    next >
Text File  |  1990-04-12  |  5KB  |  101 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $flatten1.P */
  25.  
  26. /* **********************************************************************
  27. $flatten1_export([$add_uni_hdr/6,$flatten1/8]).
  28.  
  29. $flatten1_use($blist,[$append/3,_,$member1/2]).
  30. $flatten1_use($listutil1,[_,_,_,_,_,_,$member2/2,_]).
  31. $flatten1_use($glob,[_,$gennum/1,$gensym/2]).
  32. $flatten1_use($meta,[_,_,$length/2]).
  33. ********************************************************************** */
  34.  
  35.  
  36. /* "$add_uni_hdr" adds the unifications resulting from the flattening of
  37.     nested structures in the head to the body of the clause.        */
  38.  
  39. $add_uni_hdr([],_,_,_,TGoal, TGoal).
  40. $add_uni_hdr([(V=S)|UniRest],A,NVars,Vlist,
  41.         TGoal, and('_call'(=,[V,S],NV), _, UGoal1)) :-
  42.     V = v(Vid,Prag), $member1(v(Vid,OccList),Vlist), Prag = vrec(_,_,_,_),
  43.     $gennum(Vno), $member1(o(Vno,1,1,A,t,Prag),OccList),
  44.     A1 is A+1, $member1(nv(NVars),NV),
  45.     $add_uni_hdr(UniRest,A1,NVars,Vlist,TGoal, UGoal1).
  46.  
  47. /*     "flatten" flattens each top-level argument, if necessary.    */
  48.  
  49. $flatten(Var, _, _, _, _, Var, _, _) :- Var = v(_, _).
  50. $flatten([Funcsym|ArgList],Vlist,Path,Lit,Arg,
  51.             [Funcsym|NArgList],UniList,BindList) :-
  52.     $flatten1(ArgList,Vlist,Path,Lit,Arg,NArgList,UniList,BindList).
  53.  
  54. /* "$flatten1" is given a list of the arguments of any structure at the top
  55.    level.  Any structure found in this list is nested and has to be flattened.
  56.    This flattening is done by flatten2, flatten1 just loops through calling
  57.    flatten2 with each of the arguments in the list given.        */
  58.  
  59. $flatten1([], _,_, _, _, [], _,_).
  60. $flatten1([H|T],Vlist,Path,Lit,Arg, [FH|FT], UniList,BindList) :-
  61.     $flatten2(H,Vlist,Path,Lit,Arg, FH, UniList,BindList),
  62.     $flatten1(T, Vlist,Path,Lit,Arg, FT, UniList,BindList).
  63. $flatten2(Var, _,_,_, _, Var, _,_) :- Var = v(_, _), !.
  64. $flatten2(Cons,_,_,_, _, Cons, _,_) :-
  65.     $length(Cons, L),
  66.     L < 2,
  67.     !.
  68. $flatten2(Str, Vlist, Path, Lit,Arg, v(Temp, Prag), UniList,BindList) :-
  69.     $flatten_make_var(Str,Vlist,UniList,
  70.                 BindList,Path,Lit,Arg,v(Temp,Prag),FStr),
  71.     $flatten(Str, Vlist, Path, Lit, Arg, FStr, UniList,BindList).
  72.  
  73. /* "$flatten_make_var" is called with a structure Str, and returns a variable
  74.     that replaces it in the process of flattening of nested structures. It
  75.     first searches BindList to see whether the structure has already been
  76.     flattened: if so, it returns the variable that had replaced it earlier,
  77.     and enters occurrence info for the variable, as well as other such
  78.     variables inside the flattened structure, into the symbol table: this may
  79.     result in some temporary variables becoming permanent. If no such variable
  80.     is found, a new one is generated.                    */
  81.  
  82. $flatten_make_var(Str,Vlist,UniList,BindList,Path,Lit,Arg,v(Vid,Prag),_) :-
  83.         $member2((v(Vid, _),Str1),BindList),
  84.         $flatten_same_str(Str, Str1), Prag = vrec(_,_,_,_),
  85.         $member1(v(Vid, OccList), Vlist), $gennum(N),
  86.         $member1(o(N,Path,Lit,Arg,s,Prag), OccList).
  87.  
  88. $flatten_make_var(Str,Vlist,UniList,BindList,Path,Lit,Arg,v(Vid,Prag),FStr) :-
  89.         $gensym('$',Vid), $gennum(N),
  90.         $member1(v(Vid, [o(N,Path,Lit,Arg,s,Prag)|_]), Vlist),
  91.         $member1((v(Vid,_),Str),BindList), Prag = vrec(_,_,_,_),
  92.         $member1((v(Vid,_) = FStr), UniList).
  93.  
  94. $flatten_same_str([], []).
  95. $flatten_same_str(v(Vid, X), v(Vid, Y)).
  96. $flatten_same_str([Func|Args1], [Func|Args2]) :-
  97.     $flatten_same_str(Args1, Args2).
  98.  
  99. /* ---------------------------- $flatten1.P ---------------------------- */
  100.  
  101.